Conditions | 2 |
Paths | 32 |
Total Lines | 67 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | $(function(){ |
||
6 | $("body").delegate("form[data-role='ajax-request']", "submit", function(event) |
||
7 | { |
||
8 | event.preventDefault(); |
||
9 | |||
10 | var formObject = $(this); |
||
11 | |||
12 | formObject.addClass('loading'); |
||
13 | formObject.find("input").attr("readonly", "readonly"); |
||
14 | formObject.find("select").attr("readonly", "readonly"); |
||
15 | formObject.find("button[type='submit']").attr("disabled", "disabled"); |
||
16 | |||
17 | var url = $(this).attr('action'); |
||
18 | var type = $(this).attr('method'); |
||
19 | var box = $(this).attr('data-response'); |
||
20 | var data = $(this).attr('data-object'); |
||
21 | |||
22 | var call = eval($(this).attr('data-callback')) || {}; |
||
|
|||
23 | |||
24 | call.complete = call.complete || new Function(); |
||
25 | call.success = call.success || new Function(); |
||
26 | call.before = call.before || new Function(); |
||
27 | call.error = call.error || new Function(); |
||
28 | |||
29 | var form_data = $(this).serializeArray(); |
||
30 | |||
31 | var parsed = eval(data); |
||
32 | |||
33 | for (var i in parsed) |
||
34 | { |
||
35 | form_data.push({ name: i, value: parsed[i] }); |
||
36 | } |
||
37 | |||
38 | $.ajax({ |
||
39 | url: url, |
||
40 | type: type, |
||
41 | data: form_data, |
||
42 | beforeSend: function() { |
||
43 | var loader = "<div class='ui active inline loader'></div>"; |
||
44 | $(box).html(loader); |
||
45 | call.before(); |
||
46 | }, |
||
47 | error: function(jqXHR, textStatus, errorThrown) |
||
48 | { |
||
49 | $(box).html("Error processing request!. " + errorThrown); |
||
50 | |||
51 | var e = {}; |
||
52 | e.jqXHR = jqXHR; |
||
53 | e.textStatus = textStatus; |
||
54 | e.errorThrown = errorThrown; |
||
55 | |||
56 | call.error(e); |
||
57 | }, |
||
58 | success: function(data) |
||
59 | { |
||
60 | $(box).html(data); |
||
61 | call.success(data); |
||
62 | }, |
||
63 | complete: function(data) |
||
64 | { |
||
65 | formObject.find("input").removeAttr("readonly"); |
||
66 | formObject.find("select").removeAttr("readonly"); |
||
67 | formObject.find("button[type='submit']").removeAttr("disabled"); |
||
68 | formObject.removeClass('loading'); |
||
69 | call.success(data); |
||
70 | } |
||
71 | }); |
||
72 | }); |
||
73 | |||
134 | }); |